//
//  LabelTableViewCell.m
//  NewMusic
//
//  Created by dllo on 15/11/20.
//  Copyright © 2015年 Going. All rights reserved.
//

#import "LabelTableViewCell.h"
#import "CustomerNetworking.h"
#import <SDImageCache.h>
@interface LabelTableViewCell ()
@property (nonatomic, retain) UIButton *button;
@property (nonatomic, retain) UILabel *label;
@property (nonatomic, retain) UIImageView *imageV;
@property (nonatomic, retain) UIImageView *imageViewHead;
@property (nonatomic, retain) UIView *view;
@property (nonatomic, retain) UIView *viewLine;
@property (nonatomic, assign) BOOL TapFlag;
@property (nonatomic, assign) BOOL BtnFlag;
@property (nonatomic, retain) UILabel *labelNum;
@end
@implementation LabelTableViewCell

- (void)dealloc
{
    [_imageV release];
    [_label release];
    [_button release];
    [_labelName release];
    [_imageViewHead release];
    [_viewLine release];
    [_labelNum release];
    [super dealloc];
}

- (void)layoutSubviews
{
    [super layoutSubviews];
    // 赋值
    self.label.text = self.labelName;
    self.imageViewHead.image = [UIImage imageNamed:self.picName];
    // 放置控件
    [self.contentView addSubview:self.label];
    [self.contentView addSubview:self.imageViewHead];
    [self.contentView addSubview:self.view];
    if (self.num != 4 && self.num != 5)
    {
        [self.contentView addSubview:self.viewLine];
    }
    if (0 == self.num || 4 == self.num || 5 == self.num)
    {
        [self.contentView addSubview:self.imageV];
    } else {
        [self.contentView addSubview:self.button];
    }
    if (4 == self.num)
    {
        [self.contentView addSubview:self.labelNum];
        [self getCache];
    }
}
- (void)getCache
{
    NSString *file = NSSearchPathForDirectoriesInDomains(NSCachesDirectory, NSUserDomainMask, YES).lastObject;
    CGFloat SDsize = [[SDImageCache sharedImageCache] getSize];
    CGFloat AFNsize = [CustomerNetworking floatWithPath:file];
    CGFloat total = SDsize + AFNsize;
    if (total < 0.1)
    {
        self.labelNum.text = @"00.0M";
    } else {
        self.labelNum.text = [NSString stringWithFormat:@"%.2fM", total];
    }
}
- (void)labelToZero
{
    self.labelNum.text = @"0.00M";
    [[SDImageCache sharedImageCache] clearDisk];
    [CustomerNetworking removeCache];
}
- (void)buttonAction:(UIButton *)sender
{
    sender.selected = !sender.selected;
    if (1 == self.num)
    {
        if (!self.BtnFlag)
        {
            [self.delegate BtnChangeSelected:2];
            self.BtnFlag = YES;
            self.labelNum.text = @"00:00";
            [self.contentView addSubview:self.labelNum];
        } else {
            self.BtnFlag = NO;
            [self.delegate BtnChangeSelected:0];
            [self.labelNum removeFromSuperview];
        }
    }
}
- (void)changeLabelNumWith:(NSInteger)num
{
    self.labelNum.text = [NSString stringWithFormat:@"%ld:%.2ld", num/60, num%60];
}
- (void)tapAction
{
    if (!self.TapFlag)
    {
        [UIView animateWithDuration:.5f animations:^{
            self.imageV.transform = CGAffineTransformMakeRotation(M_PI / 2);
        } completion:^(BOOL finished) {
            self.TapFlag = YES;
        }];
        [self.delegate TapchangeSelected:1];
    } else {
        [UIView animateWithDuration:.5f animations:^{
            self.imageV.transform = CGAffineTransformMakeRotation(0);
        } completion:^(BOOL finished) {
            self.TapFlag = NO;
        }];
        [self.delegate TapchangeSelected:0];
    }
}
- (UIImageView *)imageViewHead
{
    if (nil == _imageViewHead)
    {
        self.imageViewHead = [[UIImageView alloc] initWithFrame:CGRectMake(10, 20, 20, 20)];
        [_imageViewHead release];
    }
    return _imageViewHead;
}
- (UIImageView *)imageV
{
    if (nil == _imageV)
    {
        self.imageV = [[UIImageView alloc] initWithFrame:CGRectMake(250, 20, 20, 20)];
        self.imageV.image = [UIImage imageNamed:@"BF-you"];
        [_imageV release];
        if (4 != self.num && 5 != self.num)
        {
            UITapGestureRecognizer *tap = [[UITapGestureRecognizer alloc] initWithTarget:self action:@selector(tapAction)];
            self.imageV.userInteractionEnabled = YES;
            [self.imageV addGestureRecognizer:tap];
            [tap release];
        }
    }
    return _imageV;
}
- (UIButton *)button
{
    if (nil == _button)
    {
        self.button = [UIButton buttonWithType:UIButtonTypeCustom];
        self.button.frame = CGRectMake(240, 5, 50, 50);
        [self.button setImage:[UIImage imageNamed:@"button-off"] forState:UIControlStateNormal];
        [self.button setImage:[UIImage imageNamed:@"button-on"] forState:UIControlStateSelected];
        [self.button addTarget:self action:@selector(buttonAction:) forControlEvents:UIControlEventTouchUpInside];
    }
    return _button;
}
- (UILabel *)labelNum
{
    if (nil == _labelNum)
    {
        self.labelNum = [[UILabel alloc] initWithFrame:CGRectMake(180, 5, 70, 50)];
        self.labelNum.textColor = [UIColor colorWithRed:205 / 255.0 green:205 / 255.0 blue:205 / 255.0 alpha:1.0];
        self.labelNum.font = [UIFont systemFontOfSize:16];
        [_labelNum release];
    }
    return _labelNum;
}
- (UILabel *)label
{
    if (nil == _label)
    {
        self.label = [[UILabel alloc] initWithFrame:CGRectMake(40, 5, 100, 50)];
        self.label.font = [UIFont systemFontOfSize:17];
        self.label.textColor = [UIColor colorWithRed:205 / 255.0 green:205 / 255.0 blue:205 / 255.0 alpha:1.0];
        [_label release];
    }
    return _label;
}
- (UIView *)view
{
    if (nil == _view)
    {
        self.view = [[UIView alloc] initWithFrame:CGRectMake(0, 0, 300, 60)];
        self.view.backgroundColor = [UIColor whiteColor];
        self.view.alpha = 0.05;
        [_view release];
    }
    return _view;
}
- (UIView *)viewLine
{
    if (nil == _viewLine)
    {
        self.viewLine = [[UIView alloc] initWithFrame:CGRectMake(40, 59, 260, 1)];
        self.viewLine.backgroundColor = [UIColor colorWithRed:150 / 255.0 green:150 / 255.0 blue:150 / 255.0 alpha:1.0];
        [_viewLine release];
    }
    return _viewLine;
}
- (void)awakeFromNib {
    // Initialization code
}

- (void)setSelected:(BOOL)selected animated:(BOOL)animated {
    [super setSelected:selected animated:animated];
    
    // Configure the view for the selected state
}

@end
